package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.Collage;
import com.yugong.entity.dto.Major;
import com.yugong.entity.vo.request.CollageRequestVO;
import com.yugong.entity.vo.response.CollageListResponseVO;
import com.yugong.mapper.CollageMapper;
import com.yugong.mapper.MajorMapper;
import com.yugong.service.CollageService;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学院业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/11
 */
@Service
public class CollageServiceImpl extends ServiceImpl<CollageMapper, Collage> implements CollageService {
    @Resource
    private CollageMapper collageMapper;
    @Resource
    private MajorMapper majorMapper;
    @Resource
    private TimeUtils timeUtils;

    /**
     * 根据学院名查询学院
     *
     * @param pageNum     当前页
     * @param pageSize    当前页数据总数
     * @param collageName 学院名
     * @return 学院
     */
    @Override
    public PageInfo<CollageListResponseVO> findCollage(Integer pageNum, Integer pageSize, String collageName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Collage> collageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageLambdaQueryWrapper
                .like(Collage::getCollageName, collageName)
                .orderByDesc(Collage::getCollageCreateTime);
        List<Collage> collageList = collageMapper.selectList(collageLambdaQueryWrapper);
        QueryWrapper<Collage> collageQueryWrapper = new QueryWrapper<>();
        collageQueryWrapper.like("collage_name", collageName);
        Long collageCountNumber = collageMapper.selectCount(collageQueryWrapper);
        List<CollageListResponseVO> collageListResponseVOList = new ArrayList<>();
        for (Collage collage : collageList) {
            CollageListResponseVO collageListResponseVO = new CollageListResponseVO();
            collageListResponseVO.setCollageNo(collage.getCollageNo());
            collageListResponseVO.setCollageName(collage.getCollageName());
            collageListResponseVO.setCollageCreateMan(collage.getCollageCreateMan());
            String createTime = timeUtils.setDateToStrDate(collage.getCollageCreateTime(), Const.YYYY_MM_DD);
            collageListResponseVO.setCollageCreateTime(createTime);
            if (collage.getCollageUpdateMan() == null) {
                collageListResponseVO.setCollageUpdateMan("未更新");
            } else {
                collageListResponseVO.setCollageUpdateMan(collage.getCollageUpdateMan());
            }
            if (collage.getCollageUpdateTime() == null) {
                collageListResponseVO.setCollageUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(collage.getCollageUpdateTime(), Const.YYYY_MM_DD);
                collageListResponseVO.setCollageUpdateTime(updateTime);
            }
            collageListResponseVO.setCollageCountNumber(collageCountNumber);
            collageListResponseVOList.add(collageListResponseVO);
        }
        return PageInfo.of(collageListResponseVOList);
    }

    /**
     * 查询学院
     *
     * @return 学院
     */
    @Override
    public List<CollageListResponseVO> findCollage() {
        LambdaQueryWrapper<Collage> collageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Collage> collageList = collageMapper.selectList(collageLambdaQueryWrapper);
        QueryWrapper<Collage> collageQueryWrapper = new QueryWrapper<>();
        Long collageCountNumber = collageMapper.selectCount(collageQueryWrapper);
        List<CollageListResponseVO> collageListResponseVOList = new ArrayList<>();
        for (Collage collage : collageList) {
            CollageListResponseVO collageListResponseVO = new CollageListResponseVO();
            collageListResponseVO.setCollageNo(collage.getCollageNo());
            collageListResponseVO.setCollageName(collage.getCollageName());
            collageListResponseVO.setCollageCreateMan(collage.getCollageCreateMan());
            String createTime = timeUtils.setDateToStrDate(collage.getCollageCreateTime(), Const.YYYY_MM_DD);
            collageListResponseVO.setCollageCreateTime(createTime);
            if (collage.getCollageUpdateMan() == null) {
                collageListResponseVO.setCollageUpdateMan("未更新");
            } else {
                collageListResponseVO.setCollageUpdateMan(collage.getCollageUpdateMan());
            }
            if (collage.getCollageUpdateTime() == null) {
                collageListResponseVO.setCollageUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(collage.getCollageUpdateTime(), Const.YYYY_MM_DD);
                collageListResponseVO.setCollageUpdateTime(updateTime);
            }
            collageListResponseVO.setCollageCountNumber(collageCountNumber);
            collageListResponseVOList.add(collageListResponseVO);
        }
        return collageListResponseVOList;
    }

    /**
     * 新增学院
     *
     * @param collageRequestVO 学院请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int addCollage(CollageRequestVO collageRequestVO) {
        Collage collage = new Collage();
        collage.setCollageNo(collageRequestVO.getCollageNo());
        collage.setCollageName(collageRequestVO.getCollageName());
        collage.setCollageCreateMan(collageRequestVO.getCollageCreateMan());
        collage.setCollageCreateTime(new Date());
        return collageMapper.insert(collage);
    }

    /**
     * 修改学院
     *
     * @param collageRequestVO 学院请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateCollage(CollageRequestVO collageRequestVO) {
        Collage collage = new Collage();
        collage.setCollageNo(collageRequestVO.getCollageNo());
        collage.setCollageName(collageRequestVO.getCollageName());
        collage.setCollageUpdateMan(collageRequestVO.getCollageUpdateMan());
        collage.setCollageUpdateTime(new Date());
        return collageMapper.updateById(collage);
    }

    /**
     * 删除学院
     *
     * @param collageNo 学院号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCollage(String collageNo) {
        LambdaQueryWrapper<Major> majorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        majorLambdaQueryWrapper.eq(Major::getMajorCollageNo, collageNo);
        List<Major> majorList = majorMapper.selectList(majorLambdaQueryWrapper);
        if (majorList == null) {
            return collageMapper.deleteById(collageNo);
        } else {
            int deleteMajorFlag = 0;
            int deleteFlag = 0;
            for (Major major : majorList) {
                deleteMajorFlag = majorMapper.deleteById(major.getMajorNo());
                deleteFlag = deleteFlag + deleteMajorFlag;
            }
            int deleteCollageFlag = collageMapper.deleteById(collageNo);
            return deleteFlag + deleteCollageFlag;
        }
    }
}
